﻿using System;
using System.Linq;
using UnboundStructures.Model;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace UnboundStructures
{
    public class Program
    {
        public static void Main()
        {
            // Loading domain configuration section with name "Default".
            var domainConfiguration = DomainConfiguration.Load("Default");
            var domain = Domain.Build(domainConfiguration);

            using (var session = domain.OpenSession()) {
                using (var tx = session.OpenTransaction()) {

                    // Unbound structures can be created without hosted Entity
                    // But they do need Session instance as well
                    var address = new Address(session) {
                        Country = "USA", 
                        State = "CA", 
                        City = "Mountain View", 
                        Street1 = "1600 Amphitheatre Parkway", 
                        Zip = "94043"
                    };

                    // They can be assigned to correspondent fields
                    var google = new Company(session) {
                        Name = "Google Inc.",
                        Address = address
                    };

                    PrintCompany(google);

                    // Unbound structures can be used in queries
                    // All field will be used to form 'where' clause
                    Console.WriteLine("\n * Using structures in queries");
                    var companies = session.Query.All<Company>()
                        .Where(c => c.Address == address).ToList();
                    foreach (var company in companies)
                        PrintCompany(company);

                    tx.Complete();
                }
            }
        }

        private static void PrintCompany(Company company)
        {
            Console.WriteLine("Company: {0}, {1} {2}, {3} {4} {5}", company.Name, company.Address.Street1, company.Address.City, company.Address.State, company.Address.Zip, company.Address.Country);            
        }
    }
}